WEBVTT

00:01.000 --> 00:06.610
In this center next videos you will learn how to create sliding windows are moving windows to calculate

00:06.610 --> 00:11.290
rolling statistics like simple moving average as slow and finance.

00:11.290 --> 00:17.920
It is tradition to work with rolling statistics that are often also called financial indicators or financial

00:17.920 --> 00:24.190
studies and rolling statistics are actually heavily used in particular and technical chart analysis

00:24.520 --> 00:27.380
but also for performance reporting.

00:27.400 --> 00:31.290
So what do I mean with moving windows and rolling statistics.

00:31.300 --> 00:38.380
Let's assume that we have daily prices for the S&amp;P 500 index for the last 10 years and we want to know

00:38.380 --> 00:39.780
on each and every day.

00:39.790 --> 00:47.290
The mean or the average stock price of the directly preceding 50 days so on each and every day our window

00:47.290 --> 00:55.180
with 50 daily timestamps moves forward by one day covering their respective day and the forty nine directly

00:55.180 --> 00:58.200
preceding days and from the set.

00:58.300 --> 01:04.870
That we can calculate the mean stock price or the median or the max or the min so the statistic is actually

01:04.870 --> 01:06.180
flexible here.

01:06.370 --> 01:09.800
And by doing so we can actually smooth our data.

01:10.030 --> 01:14.840
But we are also able to capture some recent trends in our price data.

01:14.950 --> 01:17.080
So let's start with an example.

01:17.080 --> 01:24.820
This is always the easiest way and we import pandas and met plop lip and we use the Seabourn style and

01:24.820 --> 01:32.450
then we are importing S&amp;P 500 price data from the CSC profile S&amp;P 500 dot see a sphere.

01:32.470 --> 01:41.050
And as always we try to convert the daytime information to a daytime data type with past dates and we

01:41.050 --> 01:43.260
want to have a date time index.

01:43.300 --> 01:47.770
And also here we pass day to the next column so let's do this.

01:47.770 --> 01:56.180
And by doing so we're actually saving our data frame in the variable S&amp;P 500 and 7.

01:56.500 --> 02:04.450
So these are the first five timestamps starting in the year nineteen hundred seventy and onto the year

02:04.480 --> 02:06.090
2000 and 18.

02:06.100 --> 02:07.690
So the end of the year.

02:07.840 --> 02:12.110
And let's also have a look here at the info method.

02:12.280 --> 02:18.610
So we have twelve thousand one hundred and seven timestamps here and a day time index.

02:18.640 --> 02:22.470
Now let's assume that we only want to work with the closing prices.

02:22.630 --> 02:25.330
So we select the column close.

02:25.360 --> 02:32.080
So this would be a pen series and we can transform a panda series where data frame with the method to

02:32.080 --> 02:36.930
frame and by doing so we're actually overwriting here our variable.

02:37.000 --> 02:38.140
Let's do this.

02:38.140 --> 02:39.490
And let's again have a look here.

02:40.090 --> 02:48.930
So these are the first five timestamps with the first five closing prices of the S&amp;P 500 index.

02:48.940 --> 02:56.470
Now we can also create a chart or a plot with the historic prices by using here the plot method and

02:56.470 --> 02:59.820
the use of fixed size of twelve eight and a font size of 15.

03:00.370 --> 03:02.820
And we also want FBA legend.

03:02.830 --> 03:03.840
So let's have a look here.

03:05.190 --> 03:06.150
So this is here.

03:06.150 --> 03:07.770
Our price chart.

03:07.980 --> 03:14.640
And apparently it wasn't too bad idea to invest in the S&amp;P 500 index in the 70s.

03:14.670 --> 03:22.510
So by doing so we would have increased our investment from let's say 100 to about 2000.

03:22.770 --> 03:27.660
300 and this is still the performance without dividends.

03:27.660 --> 03:33.720
So we do not use yet the adjusted clause but only the clothes and let us decide that we only want to

03:33.720 --> 03:41.340
focus on the last 10 years and select only the timestamps from the thirty first of December 2008 and

03:41.340 --> 03:46.400
to the last day of the year 2000 and 18 and be over right.

03:46.410 --> 03:54.490
Actually our variable here and let's assume that for each and every day of the last 10 years you want

03:54.490 --> 03:58.710
to have a rolling window of 10 times stands for 10 days.

03:58.720 --> 04:03.590
So on each and every day you want to look back on the last nine days.

04:03.610 --> 04:05.710
And actually the respective day.

04:06.010 --> 04:13.670
So this is actually then a moving windows with the main window 10 and in Penang we can actually do this

04:13.670 --> 04:15.530
with the rolling method.

04:15.800 --> 04:23.510
So we have our data from S&amp;P 500 and then we will see the rolling method and we can pass the a 10 to

04:23.510 --> 04:27.240
the windows power meter so let's uh have a look here.

04:27.440 --> 04:35.450
And by doing so we're actually creating a new object of the type or the class Panda's car windows rolling.

04:35.450 --> 04:41.690
So in the background for each and every time stamp panels actually aggregates the directly preceding

04:41.690 --> 04:46.300
a 10 timestamp so we have a rolling window of 10.

04:46.520 --> 04:51.810
And that's again have a look here at the first 15 rows of our S&amp;P 500 data frame.

04:51.860 --> 04:59.370
So this might be quite helpful in a few minutes so we are starting here with the very last day in 2008

04:59.430 --> 05:03.680
and then we have the second of January 2009.

05:03.720 --> 05:09.870
Now let's assume that we want to create here a rolling window of 10 and we are doing the say actually

05:09.870 --> 05:16.230
by applying the rolling method and then we have to change another method we have we have to define how

05:16.230 --> 05:19.100
we want to aggregate actually our window.

05:19.140 --> 05:26.430
So we have actually on each time stamp a set of 10 timestamps and we actually have to define which statistic

05:26.460 --> 05:28.020
we want to calculate.

05:28.020 --> 05:34.170
And for example we can change the mean method and then we are actually calculating the simple moving

05:34.170 --> 05:34.790
average.

05:34.800 --> 05:44.530
So let's do this here and we can see here that um our new data frame starts here with the any end values.

05:44.780 --> 05:52.100
And then here on the 14th of January we have actually the very first the simple moving average or the

05:52.100 --> 05:58.660
very first rolling statistic so apparently on the first nine times stamps welfare and a general use.

05:58.810 --> 06:02.810
And this is simply the case so let's have a look at the very first time stamp.

06:02.830 --> 06:07.300
So here we do not have um the 10 preceding time stamps.

06:07.330 --> 06:11.860
So we only have here one time stamp then for the second time stamp.

06:11.860 --> 06:13.770
We only have two time stamps.

06:13.780 --> 06:18.250
Father rolling statistics then for the third time stamp.

06:18.250 --> 06:22.420
We only have three preceding timestamps and data.

06:23.020 --> 06:27.770
So we cannot calculate a rolling statistic with a window of 10.

06:27.850 --> 06:34.920
And then finally we have the 14th of January it's the tenth time stamp and here we have in total 10

06:35.070 --> 06:38.410
timestamps that are immediately preceding.

06:38.580 --> 06:45.150
And here we are able actually to calculate uh the mean stock price for these 10 times stamps and instead

06:45.150 --> 06:48.510
of calculating the mean we can also calculate the median.

06:48.510 --> 06:50.610
So this is no problem.

06:50.610 --> 06:55.230
And of course we could also calculate uh the maximum.

06:55.230 --> 07:03.630
So the highest stock price of the preceding ten days that's the max method also the minimum with the

07:03.630 --> 07:08.770
main method.

07:08.780 --> 07:10.970
So now let's go back to the million

07:15.130 --> 07:22.560
and actually we have seen here for the first nine time stamps that we get any n values because uh there's

07:22.780 --> 07:30.580
another parameter for the rolling method that is the min periods parameter and by default the argument

07:30.580 --> 07:33.310
for the min periods parameter is actually the same mass.

07:33.310 --> 07:36.760
So the argument for the windows power meter.

07:36.760 --> 07:44.910
So if you pass tend to the windows parameter then by default the min periods parameter is also 10 so

07:44.940 --> 07:46.920
let's have a look at the documentation

07:49.950 --> 07:54.350
so we have here the windows parameter and then the min periods parameter.

07:54.360 --> 07:59.150
And it sets the minimum number of observations and window required to have a value.

07:59.160 --> 08:02.190
Otherwise the result is an A.

08:02.190 --> 08:08.790
So alternatively we could also say that we need a minimum number of observations of a five to calculate

08:08.820 --> 08:15.620
our rolling statistics so let's have a look here and then we can see here that only for the first the

08:15.660 --> 08:22.370
fourth time stamps we have here and well use and then for the fifth time stamp we have in total five

08:22.470 --> 08:24.860
immediately preceding timestamps.

08:25.050 --> 08:31.450
And therefore as we set here minimum periods to five then we can calculate here a rolling statistic.

08:32.160 --> 08:39.540
So this was the introduction on moving windows rolling statistics and simple moving averages and then

08:39.540 --> 08:42.040
the next videos we are going deeper into this topic.

08:42.050 --> 08:43.590
So hope to see you there by.
